import matplotlib.pyplot as plt


def plot_data():
    data = []
    with open('./output/C_output.txt', 'r') as file:
        for line in file.readlines():
            line = line.strip('\n')
            line = line.split()
            data.append(line)

    # read in points
    knots = []
    true_value = []
    s21 = []
    s32 = []
    knots.append(data[2])
    true_value.append(data[4])
    s21.append(data[6])
    s32.append(data[8])
    x = []
    y = []
    z1 = []
    z2 = []
    for k in range(0, len(knots[0])):
        x.append(float(knots[0][k]))
        y.append(float(true_value[0][k]))
        z1.append(float(s21[0][k]))
        z2.append(float(s32[0][k]))

    plt.plot(x, y, label='f(x)')
    plt.plot(x, z1, label='s21')
    plt.plot(x, z2, label='s32')
    plt.title('s21 and s32 cardinal bspline')
    plt.legend()
    plt.savefig("./pic/C.png")
    plt.show()


def plot_error():
    error = []
    with open('./output/D_output.txt', 'r') as file:
        for line in file.readlines():
            line = line.strip('\n')
            line = line.split()
            error.append(line)

    error_points = []
    error_qua = []
    error_cub = []
    error_points.append(error[1])
    error_qua.append(error[3])
    error_cub.append(error[5])

    x = []
    y1 = []
    y2 = []
    for k in range(0, len(error_points[0])):
        x.append(float(error_points[0][k]))
        y1.append(float(error_qua[0][k]))
        y2.append(float(error_cub[0][k]))

    plt.plot(x, y1, label='s21 error')
    plt.plot(x, y2, label='s32 error')
    plt.title('s21 and s32 cardinal bspline error')
    plt.legend()
    plt.savefig("./pic/C_error.png")
    plt.show()


plot_data()
plot_error()
